package com.cq.sphtech.flow;


import com.cq.sphtech.model.FlowInfo;
import com.cq.sphtech.net.LoadTasksCallBack;
import com.cq.sphtech.net.NetTask;

import org.reactivestreams.Subscription;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


public class FlowInfoPresenter implements FlowInfoContract.Presenter, LoadTasksCallBack<FlowInfo> {
    private NetTask netTask;

    private FlowInfoContract.View addTaskView;

    private CompositeDisposable mSubscriptions;

    private Disposable subscription;

    public FlowInfoPresenter(NetTask netTask, FlowInfoContract.View addTaskView) {
        this.netTask = netTask;
        this.addTaskView = addTaskView;
        mSubscriptions = new CompositeDisposable();
    }

    @Override
    public void getIdInfo(String id) {
       subscription= netTask.execute(id,this);
       subscribe();

    }

    @Override
    public void onSuccess(FlowInfo flowInfo) {
        if(addTaskView.isActive()){
            addTaskView.setInfo(flowInfo);
        }

    }

    @Override
    public void onStart() {
        if (addTaskView.isActive()) {
            addTaskView.showLoading();
        }

    }

    @Override
    public void onFailed() {
        if (addTaskView.isActive()) {
            addTaskView.showError();
            addTaskView.hideLoading();
        }
    }

    @Override
    public void onFinish() {
        if (addTaskView.isActive()) {
            addTaskView.hideLoading();
        }
    }

    @Override
    public void subscribe() {
        if(subscription != null){
            mSubscriptions.add(subscription);
        }
    }

    @Override
    public void unsubscribe() {
        if(mSubscriptions != null){
            mSubscriptions.clear();
        }
    }
}
